﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string uid = textBox1.Text;
            string pwd = textBox2.Text;


            var conString = "server = .; uid = sa; pwd = seventeen111; database = CwordDB";
            SqlConnection connection = new SqlConnection(conString);
            SqlCommand command = new SqlCommand
            {
                Connection = connection,
                CommandText = "select * from Users where name = @name"
            };
            command.Parameters.AddWithValue("@name",this.textBox1.Text);
            connection.Open();
            SqlDataReader reader = command.ExecuteReader();

            //判断输入用户名和密码是否存在在数据库中
            if (reader.Read())
            {
                this.textBox1.Text = reader["name"].ToString();
                this.textBox2.Text = reader["password"].ToString();

                if(uid == this.textBox1.Text && pwd == this.textBox2.Text)
                {
                    MessageBox.Show("登录成功");
                    MainForm mainForm = new MainForm();
                    this.Hide();
                    mainForm.Show(); 
                }  
            }
            else
                {
                    MessageBox.Show("用户名或密码错误 请重新输入");
                }
            reader.Close();
            connection.Close();

        }

        private void Button2_Click(object sender, EventArgs e)
        {
            RegisterForm register = new RegisterForm();
            register.Show();
        }
    }
}
